package com.controller;

import com.bean.Cinema;
import com.service.CinemaService;
import com.util.PageAssistant;
import com.vo.CinemaVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("cinema")
public class CinemaController {
    @Autowired
    private CinemaService cinemaService;

    @RequestMapping("query")
    public String query(HttpSession session, Model model, Cinema cinema, PageAssistant<Cinema> paCna){
        System.out.println(">>>>>>>>>>> cinema query");
        System.out.println("-------------------");
        Map<String,Object> map = new HashMap<>();
        map.put("start",paCna.getStart());
        map.put("size",paCna.getSize());

        paCna.setCount(cinemaService.count(map));
        paCna.setItems(cinemaService.query(map));

        model.addAttribute("paCna",paCna);

        if(session.getAttribute("msg") != null){
            model.addAttribute("msg",session.getAttribute("msg"));
            session.removeAttribute("msg");
        }
        return "page/cinema/cinemaList";
    }

    @RequestMapping("add")
    public String add(){
        return "page/cinema/add";
    }
    @RequestMapping("save")
    public String save(HttpSession session, Model model, CinemaVo cinema){
        System.out.println(">>>>>>>> cinema save");
        System.out.println(cinema);
        System.out.println("--------------------------");
        List<Cinema> query = cinemaService.query(new HashMap<>());

        for (Cinema c: query) {
            if(cinema.getHall().equals(c.getHall())){
                model.addAttribute("msg","影厅名不能重复");
                return "page/cinema/add";
            }
        }

        int save = cinemaService.save(cinema.parse());
        if(save == 1){
            session.setAttribute("msg","添加成功");
        }else{
            model.addAttribute("msg","添加失败");
            return "page/cinema/add";
        }
        return "redirect:/cinema/query";
    }
    @RequestMapping("unique")
    public String unique(HttpSession session,Model model,Cinema cinema){
        System.out.println(">>>>>>>> cinema unique");
        System.out.println(cinema);
        System.out.println("--------------------------");
        if(session.getAttribute("msg") != null){
            model.addAttribute("msg",session.getAttribute("msg"));
            session.removeAttribute("msg");
        }
        List<Cinema> cinemaList = cinemaService.find(cinema);
        if(cinemaList.size() == 1){
            model.addAttribute("cinemaList",cinemaList.get(0));
            return "page/cinema/see";
        }
        return null;
    }
    @RequestMapping("update")
    public String update(Model model,CinemaVo cinema){
        System.out.println(">>>>>>>> cinema update");
        System.out.println(cinema);
        System.out.println("--------------------------");

        if(cinema.getHall() != null){
            List<Cinema> query = cinemaService.query(new HashMap<>());
            for (Cinema c: query) {
                if(cinema.getHall().equals(c.getHall())){
                    model.addAttribute("msg","影厅名不能重复");
                    return "redirect:/cinema/unique?id="+cinema.getId();
                }
            }
        }
        int modify = cinemaService.modify(cinema.parse());

        if(modify == 1){
            model.addAttribute("msg","修改成功");
        }else{
            model.addAttribute("msg","修改失败");
        }

        return "redirect:/cinema/unique?id="+cinema.getId();
    }
    @RequestMapping("delete")
    public String delete(HttpSession session, Cinema cinema){
        System.out.println(">>>>>>>> cinema delete");
        System.out.println(cinema);
        System.out.println("--------------------------");
        int remove = cinemaService.remove(cinema);
        System.out.println(remove);
        if(remove == 1){
            session.setAttribute("msg","删除成功");
        }else {
            session.setAttribute("msg","删除失败");
        }
        return "redirect:/cinema/query";
    }
}
